package com.uzai.console.service.tb.tbpunishorder.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.taobao.api.response.TbkScPunishOrderGetResponse;
import com.uzai.common.utils.DateFormatUtil;
import com.uzai.common.utils.DateUtil;
import com.uzai.console.component.excel.template.TbPunishOrderExcelDemo;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.remoteapi.UploadTbPunishOrderByUzaiToolBoxDto;
import com.uzai.console.dto.tb.tbpunishorder.TbPunishOrderUpLoadDto;
import com.uzai.console.entity.TbOrder;
import com.uzai.console.entity.TbPunishOrder;
import com.uzai.console.entity.TbToken;
import com.uzai.console.mapper.TbOrderMapper;
import com.uzai.console.mapper.TbPunishOrderMapper;
import com.uzai.console.mapper.TbTokenMapper;
import com.uzai.console.service.tb.tbpunishorder.TbPunishOrderAddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author jianghan
 * @date 2019-10-23 14:49
 */
@Service
public class TbPunishOrderAddServiceImpl implements TbPunishOrderAddService {

    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbPunishOrderMapper tbPunishOrderMapper;
    @Autowired
    private TbTokenMapper tbTokenMapper;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    @Override
    public void add(TbkScPunishOrderGetResponse.Result data, TbToken tbToken) {
        TbPunishOrder tbPunishOrder = new TbPunishOrder();
        tbPunishOrder.setId(IdWorker.getId());
        tbPunishOrder.setTbTokenId(tbToken.getId());
        tbPunishOrder.setMerId(tbToken.getMerId());
        tbPunishOrder.setPunishStatus(Integer.parseInt(data.getPunishStatus()));
        tbPunishOrder.setRelationId(data.getRelationId());
        tbPunishOrder.setSettleMonth(data.getSettleMonth() != null ? data.getSettleMonth().intValue() : null);
        tbPunishOrder.setTkTradeCreateTime(DateUtil.getTime(DateFormatUtil.StringToDateFormat(data.getTkTradeCreateTime(), "yyyy-MM-dd HH:mm:ss")));
        tbPunishOrder.setTbTradeId(data.getTbTradeId() + "");
        tbPunishOrder.setTkAdzoneId(data.getTkAdzoneId());
        tbPunishOrder.setTkSiteId(data.getTkSiteId());
        tbPunishOrder.setTkPubId(data.getTkPubId());
        if ("店铺淘宝客".equals(data.getViolationType())) {
            tbPunishOrder.setViolationType(1);
        } else {
            tbPunishOrder.setViolationType(2);  // 订单虚假交易
        }
        int now = DateUtil.getTime();
        tbPunishOrder.setCreateTime(now);
        tbPunishOrder.setUpdateTime(now);

        TbOrder tbOrder = tbOrderMapper.selectByChildOrderNo(data.getTbTradeId() + "", tbToken.getMerId());
        if (tbOrder != null) {
            tbPunishOrder.setOrderId(tbOrder.getId());
            tbPunishOrder.setParentOrderNo(tbOrder.getParentOrderNo());
            tbPunishOrder.setUserId(tbOrder.getUserId());
            tbPunishOrder.setNickName(tbOrder.getNickName());
            tbPunishOrder.setGoodsTitle(tbOrder.getTitle());

            //找到淘宝订单才入库处罚订单
            tbPunishOrderMapper.insertSelective(tbPunishOrder);
        }


    }

    @Override
    public void addByUpload(TbPunishOrderExcelDemo tbPunishOrderExcelDemo, TbPunishOrderUpLoadDto tbPunishOrderUpLoadDto){
        int now = DateUtil.getTime();
        TbPunishOrder tbPunishOrder = new TbPunishOrder();
        tbPunishOrder.setId(IdWorker.getId());
        tbPunishOrder.setCreateTime(now);
        tbPunishOrder.setUpdateTime(now);
        tbPunishOrder.setMerId(tbPunishOrderUpLoadDto.getMerId());
        tbPunishOrder.setSettleMonth(tbPunishOrderUpLoadDto.getSettleMonth());
        tbPunishOrder.setViolationType(tbPunishOrderUpLoadDto.getViolationType());
        tbPunishOrder.setTbTradeId(tbPunishOrderExcelDemo.getChildOrderNo());
        tbPunishOrder.setAmount(tbPunishOrderExcelDemo.getAmount());

        TbOrder tbOrder = tbOrderMapper.selectByChildOrderNo(tbPunishOrderExcelDemo.getChildOrderNo() + "", tbPunishOrderUpLoadDto.getMerId());
        if (tbOrder != null) {
            tbPunishOrder.setOrderId(tbOrder.getId());
            tbPunishOrder.setParentOrderNo(tbOrder.getParentOrderNo());
            tbPunishOrder.setUserId(tbOrder.getUserId());
            tbPunishOrder.setNickName(tbOrder.getNickName());
            tbPunishOrder.setGoodsTitle(tbOrder.getTitle());
            tbPunishOrder.setRelationId(tbOrder.getRelationId());
            tbPunishOrder.setTkTradeCreateTime(tbOrder.getCreatedt());
            tbPunishOrder.setTkAdzoneId(tbOrder.getAdzoneId());
            tbPunishOrder.setTkSiteId(tbOrder.getSiteId());
            if(tbOrder.getAlimmTbId()  != null){
                TbToken tbToken = null;
                if(uzaiConsoleSysConfig.getTbAppkeyDg().equals(tbOrder.getAppKey())){
                    tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbOrder.getAlimmTbId(), 1);
                }else{
                    tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbOrder.getAlimmTbId(), 0);
                }
                if(tbToken != null){
                    tbPunishOrder.setTbTokenId(tbToken.getId());
                }
            }

            //查询到淘宝订单，处罚订单才入库
            tbPunishOrderMapper.insertSelective(tbPunishOrder);
        }

    }

    @Override
    public void addByUploadByUzaiToolsBox(UploadTbPunishOrderByUzaiToolBoxDto.PunishOrderInfo punishOrderInfo, Long merId){
        int now = DateUtil.getTime();
        TbPunishOrder tbPunishOrder = new TbPunishOrder();
        tbPunishOrder.setId(IdWorker.getId());
        tbPunishOrder.setCreateTime(now);
        tbPunishOrder.setUpdateTime(now);
        tbPunishOrder.setMerId(merId);
        tbPunishOrder.setSettleMonth(Integer.valueOf(punishOrderInfo.getSettleMonth()));
        //处罚类型，目前包括 1:店铺淘宝客 2:订单虚假交易；3-账户虚假交易；4-订单笔交易异常；5-其他
        if("店铺淘宝客".equals(punishOrderInfo.getViolationType())){
            tbPunishOrder.setViolationType(1);
        }else if("订单虚假交易".equals(punishOrderInfo.getViolationType())){
            tbPunishOrder.setViolationType(2);
        }else if("账户虚假交易".equals(punishOrderInfo.getViolationType())){
            tbPunishOrder.setViolationType(3);
        }else if("订单笔交易异常".equals(punishOrderInfo.getViolationType())){
            tbPunishOrder.setViolationType(4);
        }else{
            tbPunishOrder.setViolationType(5);
        }

        tbPunishOrder.setTbTradeId(punishOrderInfo.getChildOrderNo());
        tbPunishOrder.setAmount(punishOrderInfo.getAmount());

        TbOrder tbOrder = tbOrderMapper.selectByChildOrderNo(punishOrderInfo.getChildOrderNo(), merId);
        if (tbOrder != null) {
            tbPunishOrder.setOrderId(tbOrder.getId());
            tbPunishOrder.setParentOrderNo(tbOrder.getParentOrderNo());
            tbPunishOrder.setUserId(tbOrder.getUserId());
            tbPunishOrder.setNickName(tbOrder.getNickName());
            tbPunishOrder.setGoodsTitle(tbOrder.getTitle());
            tbPunishOrder.setRelationId(tbOrder.getRelationId());
            tbPunishOrder.setTkTradeCreateTime(tbOrder.getCreatedt());
            tbPunishOrder.setTkAdzoneId(tbOrder.getAdzoneId());
            tbPunishOrder.setTkSiteId(tbOrder.getSiteId());
            if(tbOrder.getAlimmTbId()  != null){
                TbToken tbToken = null;
                if(uzaiConsoleSysConfig.getTbAppkeyDg().equals(tbOrder.getAppKey())){
                    tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbOrder.getAlimmTbId(), 1);
                }else{
                    tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbOrder.getAlimmTbId(), 0);
                }
                if(tbToken != null){
                    tbPunishOrder.setTbTokenId(tbToken.getId());
                }
            }

            //查询到淘宝订单，处罚订单才入库
            tbPunishOrderMapper.insertSelective(tbPunishOrder);
        }
    }
}
